<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tent extends MY_Controller {
  function __construct(){
    parent::__construct();
  }

  function index($start = 0) {
    $this->load->model('post_model','postModel');
    $this->load->model('images_model','imagesModel');
    $this->load->model('type_model','typeModel');
    $this->load->model('user_model','userModel');
    $this->load->library('pagination');
    
    //config pagination
    $config['prev_link'] = '&laquo;';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';

    $config['next_link'] = '&raquo;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';

    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';

    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';

    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    $whereUser["where_in"]["group"] = array(6,3);
    $userListData = $this->userModel->get($whereUser);
    $userListData[]["id"] = 5;
    foreach ($userListData as $key => $value) {
      $userList[] = $value["id"];
    }

    //get all the URI segments for pagination and sorting
    $segment_array = $this->uri->segment_array();
    $segment_count = $this->uri->total_segments();

    $config['base_url'] = base_url('tent');
    $whereCount["status"] = 1;
    $whereCount["where_in"]["usr_id"] = $userList;
    $config['total_rows'] = $this->postModel->count_rows($whereCount);
    $config['per_page'] = 20;

    //getting the records and limit setting
    if (ctype_digit($segment_array[$segment_count])) {
      $where['limit']['amount'] = $config['per_page'];
      if($segment_array[$segment_count] > 0){
        $where['limit']['start'] = $segment_array[$segment_count];
      }else{
        $where['limit']['start'] = 0;
      }
      $start_offset = ($segment_array[$segment_count]+1);
      $end_offer = ($segment_array[$segment_count]+$config['per_page']);
      if($end_offer > $config['total_rows']){
        $end_offer = $config['total_rows'];
      }
      array_pop($segment_array);
    } else {
      $where['limit']['amount'] = $config['per_page'];
      $where['limit']['start'] = 0;
      $start_offset = 1;
    }
    
    $config['uri_segment'] = count($segment_array)+1;
    $where["where_in"]["usr_id"] = $userList;
    $where['where']['status'] = 1;
    $where["order"] = "update_date DESC";
    $posts = $this->postModel->get($where);
    if(!empty($posts)){
      foreach ($posts as $key => $value) {
        $posts[$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
        $posts[$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
        $posts[$key]["province"] = Util::getProvince($posts[$key]["province"]);
        $posts[$key]["meta"] = Util::getPostMeta($posts[$key]["id"]);
        $posts[$key]["type"] = Util::getType($posts[$key]["typ_id"]);
        $posts[$key]["comment_count"] = Util::getCommentCount($posts[$key]["id"]);
      }
    }else{
      $posts = array();
    }

    if((count($posts)+1)>$start_offset){
      $end_offer = count($posts);
    }

    $this->pagination->initialize($config);

    $this->_assign("end_offset",$end_offer);
    $this->_assign("start_offset",$start_offset);
    $this->_assign('posts',$posts);
    $this->_fetch('user_index');
  }

}